/**
 * OptionsPanel.java
 * 
 * JPanel that contains options that the user can set for prediction. Also
 * displays the prediction values and buttons for generating the Past Data,
 * Neural Network Error and Actual Data charts.
 *  
 */

package com.swin.energy;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;

public class OptionsPanel extends JPanel implements ActionListener {

	private static final long serialVersionUID = -1742320762237800410L;

	enum PAST_DATA {
		DAY, YEAR
	};

	enum ESTIMATE_FOR {
		APPLIANCE, HOUSE, REGION, ALL
	};

	enum ACTUAL_DATA {
		HOUR, DAY, WEEK, SIX_MONTHS, YEAR
	};

	private JPanel panel0;
	private JPanel panel1;
	private JPanel panel2;
	private JPanel panel3;
	private JPanel panel4;
	private JPanel panel5;
	private JPanel panel6;
	private JPanel panel7;

	// Create components for the stats panel
	private JRadioButton seasonForMonth;
	private JRadioButton seasonForFullYear;
	private ButtonGroup seasonGroup;
	private JLabel seasonGroupLbl;

	String[] seasons = { "Winter", "Spring", "Summer", "Autumn" };

	private JRadioButton predictionForHouse;
	private JRadioButton predictionForRegion;
	private JRadioButton predictionForApps;
	private JRadioButton predictionForAll;
	private ButtonGroup predictionGroup;
	private JLabel predictionGroupLbl;

	private JRadioButton estimateForNextHour;
	private JRadioButton estimateForNextDay;
	private JRadioButton estimateForNextSixMonths;
	private JRadioButton estimateForNextYear;
	private JRadioButton estimateForNextWeek;
	private ButtonGroup estimateGroup;
	private JLabel estimateGroupLbl;

	private JButton dataUpdate;
	private JButton trainData;
	private JButton actualData;

	private JLabel currentLbl;
	private JLabel estimateLbl;
	private JLabel actualLbl;
	private JLabel medianLbl;
	private JLabel meanLbl;

	private JLabel currentValue;
	private JLabel estimateValue;
	private JLabel actualValue;
	private JLabel medianValue;
	private JLabel meanValue;
	DecimalFormat twoDecimalPlaces = new DecimalFormat("#.##");
	

	public void setMeanValue(Double val) {
		
		String text = twoDecimalPlaces.format(val);
		this.meanValue.setText(text);
	}

	public void setPastDataTotalValue(String text) {
		this.currentValue.setText(text);
	}

	public void setEstimateValueText(String text) {
		
		this.estimateValue.setText(text);
	}

	public void setActualValueText(String text) {
		this.actualValue.setText(text);
	}

	public void setMedianValue(Double val) {
		String text = twoDecimalPlaces.format(val);
		this.medianValue.setText(text);
	}

	private MainFrame frame;

	public OptionsPanel(MainFrame rootFrame) {

		this.setLayout(new BorderLayout());

		this.frame = rootFrame;

		// Create radio buttons and groups
		seasonForMonth = new JRadioButton("1 day");
		seasonForMonth.setSelected(true);
		seasonForMonth.setActionCommand("1 dat");
		seasonForFullYear = new JRadioButton("1 year");
		seasonForFullYear.setActionCommand("1 year");
		seasonGroup = new ButtonGroup();
		seasonGroup.add(seasonForMonth);
		seasonGroup.add(seasonForFullYear);
		seasonGroupLbl = new JLabel("Past Data: ");

		predictionForHouse = new JRadioButton("House");
		predictionForRegion = new JRadioButton("Region");
		predictionForRegion.setActionCommand("Region");
		predictionForApps = new JRadioButton("Appliance");
		predictionForApps.setActionCommand("Appliance");
		predictionForAll = new JRadioButton("All");
		predictionForAll.setActionCommand("All");
		predictionForAll.setSelected(true);
		predictionGroup = new ButtonGroup();
		predictionGroup.add(predictionForHouse);
		predictionGroup.add(predictionForRegion);
		predictionGroup.add(predictionForApps);
		predictionGroup.add(predictionForAll);
		predictionGroupLbl = new JLabel("Estimation fields: ");

		estimateForNextHour = new JRadioButton("Next hour");
		estimateForNextHour.setSelected(true);
		estimateForNextDay = new JRadioButton("Next day");
		estimateForNextDay.setActionCommand("Next day");
		estimateForNextWeek = new JRadioButton("Next week");
		estimateForNextWeek.setActionCommand("Next week");
		estimateForNextSixMonths = new JRadioButton("6 months");
		estimateForNextSixMonths.setActionCommand("6 months");
		estimateForNextYear = new JRadioButton("1 Year");
		estimateForNextYear.setActionCommand("1 Year");

		estimateGroup = new ButtonGroup();
		estimateGroup.add(estimateForNextDay);
		estimateGroup.add(estimateForNextWeek);
		estimateGroup.add(estimateForNextHour);
		estimateGroup.add(estimateForNextSixMonths);
		estimateGroup.add(estimateForNextYear);
		estimateGroupLbl = new JLabel("Prediction Forecast: ");

		// Create buttons and textfields
		dataUpdate = new JButton("Data");
		dataUpdate.addActionListener(this);
		trainData = new JButton("Train");
		trainData.addActionListener(this);
		actualData = new JButton("Actual");
		actualData.addActionListener(this);

		currentLbl = new JLabel("Past Energy Usgae: ");
		estimateLbl = new JLabel("Estimated Usage: ");
		actualLbl = new JLabel("Actual Usage: ");
		medianLbl = new JLabel("Median (Past Usage): ");
		meanLbl = new JLabel("Mean (Past Usage): ");

		currentValue = new JLabel();
		estimateValue = new JLabel();
		actualValue = new JLabel();
		medianValue = new JLabel();
		meanValue = new JLabel();

		// Create internal panels
		panel0 = new JPanel();
		panel1 = new JPanel();
		panel2 = new JPanel();
		panel3 = new JPanel();
		panel4 = new JPanel();
		panel5 = new JPanel();
		panel6 = new JPanel();
		panel7 = new JPanel();

		panel0.setLayout(new GridLayout(1, 5));
		panel1.setLayout(new GridLayout(4, 1));
		panel2.setLayout(new GridLayout(5, 1));
		panel3.setLayout(new GridLayout(6, 1));
		panel4.setLayout(new GridLayout(5, 1));
		panel5.setLayout(new GridLayout(5, 1));

		panel1.add(seasonGroupLbl);
		panel1.add(seasonForMonth);
		// panel1.add(seasonForHalfYear);
		panel1.add(seasonForFullYear);

		panel2.add(predictionGroupLbl);
		panel2.add(predictionForAll);
		panel2.add(predictionForRegion);
		panel2.add(predictionForHouse);
		panel2.add(predictionForApps);

		panel3.add(estimateGroupLbl);
		panel3.add(estimateForNextHour);
		panel3.add(estimateForNextDay);
		panel3.add(estimateForNextWeek);
		panel3.add(estimateForNextSixMonths);
		panel3.add(estimateForNextYear);

		panel4.add(currentLbl);
		panel4.add(estimateLbl);
		panel4.add(actualLbl);
		panel4.add(medianLbl);
		panel4.add(meanLbl);

		panel5.add(currentValue);
		panel5.add(estimateValue);
		panel5.add(actualValue);
		panel5.add(medianValue);
		panel5.add(meanValue);

		panel0.add(panel1);
		panel0.add(panel2);
		panel0.add(panel3);
		panel0.add(panel4);
		panel0.add(panel5);

		panel6.setLayout(new FlowLayout());
		panel7.setLayout(new FlowLayout());

		panel6.add(dataUpdate);
		panel6.add(trainData);
		panel6.add(actualData);

		this.add(panel0, BorderLayout.NORTH);
		this.add(panel6, BorderLayout.CENTER);
		this.add(panel7, BorderLayout.SOUTH);
		this.setBorder(new EmptyBorder(20, 20, 20, 20));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == dataUpdate) {
			frame.startPastDataThread(currentValue);
		} else if (source == trainData) {
			frame.startTrainingThread();
		} else if (source == actualData) {
			frame.startActualThread(estimateValue);
		}
	}

	public void showUpdateButton() {
		dataUpdate.setVisible(true);
	}

	public PAST_DATA getLengthOfWindow() {
		PAST_DATA time = PAST_DATA.DAY;
		if (seasonForMonth.isSelected()) {
			time = PAST_DATA.DAY;
		} else if (seasonForFullYear.isSelected()) {
			time = PAST_DATA.YEAR;
		}
		return time;
	}

	public ESTIMATE_FOR getEstimationFor() {
		ESTIMATE_FOR estimation = ESTIMATE_FOR.APPLIANCE;
		if (predictionForHouse.isSelected()) {
			estimation = ESTIMATE_FOR.HOUSE;
		} else if (predictionForRegion.isSelected()) {
			estimation = ESTIMATE_FOR.REGION;
		} else if (predictionForApps.isSelected()) {
			estimation = ESTIMATE_FOR.APPLIANCE;
		} else if (predictionForAll.isSelected()) {
			estimation = ESTIMATE_FOR.ALL;
		}
		return estimation;
	}

	public ACTUAL_DATA getActualWindowLength() {
		ACTUAL_DATA data = ACTUAL_DATA.HOUR;
		if (estimateForNextHour.isSelected()) {
			data = ACTUAL_DATA.HOUR;
		} else if (estimateForNextDay.isSelected()) {
			data = ACTUAL_DATA.DAY;
		} else if (estimateForNextWeek.isSelected()) {
			data = ACTUAL_DATA.WEEK;
		} else if (estimateForNextSixMonths.isSelected()) {
			data = ACTUAL_DATA.SIX_MONTHS;
		} else if (estimateForNextYear.isSelected()) {
			data = ACTUAL_DATA.YEAR;
		}
		return data;
	}

}